package net.sf.odinms.exttools.doclet.pages;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.sun.javadoc.PackageDoc;
import net.sf.odinms.exttools.doclet.AbstractPageWriter;
import net.sf.odinms.exttools.doclet.Doclet;

/**
 * Creates "overview-summary.html" which details documented packages.
 */
@SuppressWarnings("unchecked")
public final class OverviewSummary extends AbstractPageWriter {
	
	/**
	 * Array of Packages to be documented.
	 */
	private PackageDoc[] packages;
	
	/**
	 * Map representing the group of packages as specified on the command line.
	 * 
	 * @see com.sun.tools.doclets.internal.toolkit.util.Group
	 */
	private Map groupPackageMap;
	
	/**
	 * List to store the order groups as specified on the command line.
	 */
	private List groupList;
	
	/** Builder used to create multiple TH tags with different inner text. */
	private final TagBuilder th = new TagBuilder("th");
	/** Builder used to create multiple TD tags with different inner text. */
	private final TagBuilder td = new TagBuilder("td");
	
	/**
	 * Generate page containing a summary of packages and project description.
	 */
	public static void generateOverviewSummary() {
		String filename = "overview-summary" + CONF.ext;
		try {
			new OverviewSummary(filename);
		}
		catch (IOException exc) {
			throw Doclet.exception(exc, filename);
		}
	}
	
	/**
	 * Initialize and create overview summary page.
	 * 
	 * @param filename The desired name of the file (with extension).
	 * @throws IOException If {@link java.io.FileOutputStream} creation fails.
	 */
	private OverviewSummary(String filename) throws IOException {
		super(filename);
		pageType = PageType.OVERVIEW;
		packages = CONF.packages;
		groupPackageMap =
				(packages != null)
						? CONF.group.groupPackages(packages)
						: new java.util.HashMap();
		groupList = CONF.group.getGroupList();
		
		windowTitle = CONF.propertyText("OverviewTitle");
		String[] metakeywords = {windowTitle};
		if (CONF.doctitle.length() > 0) {
			metakeywords[0] += ", " + CONF.doctitle;
		}
		
		String setFrameScript =
				"parent.packageFrame.location.href = 'allclasses-frame"
						+ CONF.ext + "';";
		printXhtmlHeader(metakeywords, true, setFrameScript);
		
		println(open("h1") + CONF.propertyText("OverviewTitle") + close("h1"));
		
		printOverviewHeader();
		if (CONF.packages != null && CONF.packages.length > 0) {
			Arrays.sort(CONF.packages);
			
			println(open("h2") + CONF.propertyText("summary.Package")
					+ close("h2"));
			
			println(open("table"));
			for (PackageDoc pkg : packages)
				printIndexRow(pkg);
			println(close("table"));
		}
		
		printOverview();
		
		printXhtmlFooter();
		this.close();
	}
	
	/**
	 * Print the overview summary comment for this documentation. Print one line
	 * summary at the top of the page and generate a link to the description,
	 * which is generated at the end of this page.
	 */
	private void printOverviewHeader() {
		if (CONF.root.inlineTags().length > 0) {
			// printSummaryComment(root);
			println(CONF.propertyText("doclet.See"));
			println(linkToLabelHref(CONF.propertyText("Description"),
					"#overview_description"));
		}
	}
	
	/**
	 * Print each package in separate rows in the index table. Generate link to
	 * each package.
	 * 
	 * @param pkg Package to which link is to be generated.
	 */
	private void printIndexRow(PackageDoc pkg) {
		if (pkg != null && pkg.name().length() > 0) {
			println(open("tr"));
			print(th.getOpenTextClose(linkToLabelHref(pkg.name(),
					hrefToDoc(pkg) + "package-summary" + CONF.ext)));
			print(td.getOpenTextClose(getCommentSummary(pkg)));
			println(close("tr"));
		}
	}
	
	/**
	 * Print the overview comment, then generate the tag information, as
	 * directed in the file specified by the "-overview" option.
	 */
	private void printOverview() {
		if (CONF.root.inlineTags().length > 0) {
			println(openDivWithID("overview_description"));
			// printInlineComment(root);
			println(close("div"));
		}
		// printTags(root);
	}
	
	/** Highlight "Overview" as current section, don't create link. */
	protected void navLinkOverview() {
		println(listItemCurrent(OVERVIEW));
	}
	
}
